package com.sinodata.bsm.cicp.collector.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.telnet.JtaTelnet;
import com.sinodata.bsm.cicp.util.telnet.Telnet;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-13 16:18:40       wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class TelnetAgreement extends Collector {

    private static final Logger logger = Logger.getLogger(TelnetAgreement.class);

    private long time;

    public TelnetAgreement() {
        super();
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#collect(java.lang.String)
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String userName = this.getParamMap().get("Username");
        String password = this.getParamMap().get("Password");
        time = System.currentTimeMillis();
        String successPrompt = this.getParamMap().get("Login_Result_Sign");
        String port = this.getParamMap().get("Port");
        Telnet telnet = new JtaTelnet();
        try {
            telnet.connect(ip, Integer.parseInt(port));
            telnet.runLogin(userName, password, successPrompt);
            String recv = telnet.runTask("echo test");
            telnet.disconnect();
            long t = System.currentTimeMillis() - time;
            if (recv != null && recv.indexOf("test") > -1) {
                // propValues[0] = ResPropertyValue.createPropValue("YES", propMetas[0]);
                propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue(String.valueOf(t), propMetas[1]);
            } else {
                //propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
                propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue(null, propMetas[1]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (IOException e) {
            logger.error("telnet agreement error", e);
            //propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(null, propMetas[1]);
        }
        return values;
    }

    //用来读取应答的StringBuffer read（）;

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getParamMeta()
     */
    @Override
    public String[] getParamMeta() {
        //        return new String[] { "用户名", "口令", "登录成功标识","端口" };
        return new String[] { "Username", "Password", "Login_Result_Sign", "Port" };
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getPropMeta()
     */
    @Override
    public String[] getPropMeta() {
        //        return new String[] { "登录成败标志", "成功登录耗时" };
        return new String[] { "Login_Result_Sign", "Successfully_Login_Time" };
    }

}